package org.feng.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.feng.mapper.SysMenuMapper;
import org.feng.service.ISysMenuService;
import org.feng.entity.SysMenu;
import org.feng.enums.SysMenuCategoryEnum;
import org.springframework.stereotype.Service;
import org.feng.base.service.impl.BaseServiceImplNoExtendsImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 菜单表 服务实现类
 *
 * @author : FengShuai
 * @since : 2024/6/27 9:24
 */
@Service
public class SysMenuServiceImpl extends BaseServiceImplNoExtendsImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void createButton(String ids) {
        String[] idsArray = ids.split(",");
        List<SysMenu> sysMenuList = new ArrayList<>();
        for (String id : idsArray) {
            SysMenu menu = this.getById(id);
            if (menu.getCategory() == SysMenuCategoryEnum.BUTTON) {
                continue;
            }
            this.remove(Wrappers.<SysMenu>lambdaQuery().eq(SysMenu::getParentId, id));
            String code = menu.getAuthCode();
            SysMenu sysMenuAdd = SysMenu.createButton(id);
            sysMenuAdd.setName("新增");
            sysMenuAdd.setPath(code + "_add");
            sysMenuAdd.setAuthCode(code + "_add");
            sysMenuList.add(sysMenuAdd);
            SysMenu sysMenuEdit = SysMenu.createButton(id);
            sysMenuEdit.setName("编辑");
            sysMenuEdit.setPath(code + "_edit");
            sysMenuEdit.setAuthCode(code + "_edit");
            sysMenuList.add(sysMenuEdit);
            SysMenu sysMenuRemove = SysMenu.createButton(id);
            sysMenuRemove.setName("删除");
            sysMenuRemove.setPath(code + "_remove");
            sysMenuRemove.setAuthCode(code + "_remove");
            sysMenuList.add(sysMenuRemove);
        }
        this.saveBatch(sysMenuList);
    }
}

